###############################################
#                                             #
#   Experiment #2 - Election Result Reporting #
#     June 2023 sample                        #
#       PLOTS                                 #
#     Created by Mac Lockhart Nov 17 2023     #
#     Updated Nov 17 2023                     #
#                                             #
###############################################

library(tidyverse)
library(RColorBrewer)
library(car)
library(data.table)
library(modelsummary)
library(officer)
library(flextable)
library(ggpubr)

DF <- fread("National Recoded.csv")
CO <- fread("Colorado Recoded.csv")
GA <- fread("Georgia Recoded.csv")
LA <- fread("LA Recoded.csv")
TX <- fread("Texas Recoded.csv")
DF$n<-1:nrow(DF)
#########assign sample names########
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"

DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)
rm(CO, GA, LA, TX)
DF<-DF %>% mutate(trust_az_post = coalesce(trust_az_post_1, trust_az_post_2)) 
DF$trust_az <- DF$trust_az_post - DF$trust_az_pre
DF$trust_az_d <- dplyr::recode(DF$trust_az_post, "4" = "1",
                               '3' = "1",
                               '2' = "0",
                               '1' = "0")

#Treatment by party
m1.1 <- lm(trust_az_d ~ az_treatment + az_prebunk, DF[party=="Democrat"])
m1.2 <- lm(trust_az_d ~ az_treatment + az_prebunk, DF[party=="Independent"])
m1.3 <- lm(trust_az_d ~ az_treatment + az_prebunk, DF[party=="Republican"])


#Organize models into a single data frame to plot
responses <- factor(c(rep("Trust AZ elections", 6)))
sample <- factor(c(rep("Delay", 3),rep("Prebunk", 3)), 
                 levels = c("Delay", "Prebunk"))
type <- c(rep(c("Democrat", "Independent", "Republican"), 2))

trust_az_coefs_delay <-as.numeric(c(m1.1$coefficients[2], #dem value
                                    m1.2$coefficients[2], #indy value
                                    m1.3$coefficients[2])) #rep value
trust_az_coefs_prebunk <-as.numeric(c(m1.1$coefficients[3], #dem value
                              m1.2$coefficients[3], #indy value
                              m1.3$coefficients[3])) #rep value


trust_az_se<- c(sqrt(diag(vcov(m1.1)))[2],
                    sqrt(diag(vcov(m1.2)))[2],
                    sqrt(diag(vcov(m1.3)))[2],
                    sqrt(diag(vcov(m1.1)))[3],
                    sqrt(diag(vcov(m1.2)))[3],
                    sqrt(diag(vcov(m1.3)))[3])


responses_coef<- c(trust_az_coefs_delay, trust_az_coefs_prebunk)
responses_se <- c(trust_az_se)
responses_low <-responses_coef-1.28*responses_se
responses_high <-responses_coef+1.28*responses_se


temp1 <- data.frame(responses, responses_coef, responses_low, responses_high, sample, type)
p1<-ggplot(subset(temp1, temp1$sample=="Delay"), aes(fill= type,y=responses_coef, x=type,
                                                            ymax = responses_high, ymin = responses_low)) + ylim(-0.2, 0.2) +
  geom_bar(position="dodge", stat="identity", orientation="x",color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  scale_fill_grey(start = 0.5, end = 1) +
  #geom_text(aes(y = 0, label = paste0(round(vote_in_person_mean,1), "%")), position = position_dodge(width = 0.8), vjust = -0.3, size=3) +
  labs(title = "Effect of Delay Messaging", 
       color="sample", x=element_blank(), y="Coefficient", fill="Party ID") + 
  theme(axis.text = element_text(size = 12),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 14),
        axis.title.y = element_text(size = 13),
        legend.position="bottom")    
#caption = "Error bars represent 80% confidence intervals")
p2<-ggplot(subset(temp1, temp1$sample=="Prebunk"), aes(fill= type,y=responses_coef, x=type,
                                                                 ymax = responses_high, ymin = responses_low)) + ylim(-0.2, 0.2) +
  geom_bar(position="dodge", stat="identity", orientation="x",color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  scale_fill_grey(start = 0.5, end = 1) +
  #geom_text(aes(y = 0, label = paste0(round(vote_in_person_mean,1), "%")), position = position_dodge(width = 0.8), vjust = -0.3, size=3) +
  labs(title = "Effect of Prebunking Video", 
       color="sample", x=element_blank(), y="Coefficient", fill="Party ID") + 
  theme(axis.text = element_text(size = 12),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 14),
        #axis.title.y = element_text(size = 13),
        legend.position="bottom",
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.title.y=element_blank())    
#caption = "Error bars represent 80% confidence intervals")

election_preferences_plots <- ggarrange(p1, p2, nrow=1, ncol=2, heights=2, widths=2, common.legend = TRUE, legend="bottom")
election_preferences_plots<-annotate_figure(election_preferences_plots, top = text_grob("Effect of Treatments on Trust in Arizonan Elections by Partisanship", 
                                                                                        color = "black", face = "bold", size = 14),
                                            bottom = text_grob("Error bars indicate 90% confidence intervals", color= "black", size="10"))

ggsave("Figure1.png",election_preferences_plots,
       width=11, height=5)

